package com.CNMedical.system.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.diboot.core.binding.Binder;
import com.diboot.core.service.impl.BaseServiceImpl;
import com.CNMedical.common.utils.DateUtils;
import com.CNMedical.system.domain.DTO.AppletKnowledgeCommentDTO;
import com.CNMedical.system.domain.vo.AppletKnowledgeCommentVO;
import com.CNMedical.system.service.IAppletUserService;
import org.springframework.stereotype.Service;
import com.CNMedical.system.mapper.AppletKnowledgeCommentMapper;
import com.CNMedical.system.domain.entity.AppletKnowledgeComment;
import com.CNMedical.system.service.IAppletKnowledgeCommentService;

import javax.annotation.Resource;

/**
 * 知识留言Service业务层处理
 *
 * @author tang
 * @date 2023-03-29
 */
@Service
public class AppletKnowledgeCommentServiceImpl extends BaseServiceImpl<AppletKnowledgeCommentMapper, AppletKnowledgeComment>
        implements IAppletKnowledgeCommentService {
    @Resource
    private AppletKnowledgeCommentMapper appletKnowledgeCommentMapper;

    @Resource
    private IAppletUserService appletUserService;

    @Override
    public Boolean deleteCommentKnowledge(AppletKnowledgeComment appletKnowledgeComment) {
        return lambdaUpdate().eq(AppletKnowledgeComment::getKnowledgeId, appletKnowledgeComment.getKnowledgeId())
                .eq(AppletKnowledgeComment::getUserId, appletKnowledgeComment.getUserId())
                .remove();
    }

    @Override
    public List<AppletKnowledgeCommentVO> getKnowLedgeCommentByKnowledgeId(String knowledgeId) {
        // 这个方法就等价于下面的 查询entityList 然后转换为voList
        List<AppletKnowledgeCommentVO> appletKnowledgeCommentVOList = this.getViewObjectList(Wrappers.lambdaQuery(AppletKnowledgeComment.class)
                        .eq(AppletKnowledgeComment::getKnowledgeId, knowledgeId)
                        .eq(AppletKnowledgeComment::getPid, 0),
                null,
                AppletKnowledgeCommentVO.class
        );
//        List<AppletKnowledgeComment> list = lambdaQuery().eq(AppletKnowledgeComment::getKnowledgeId, knowledgeId).list();
        return appletKnowledgeCommentVOList;
    }

    /**
     * 查询知识留言
     *
     * @param id 知识留言主键
     * @return 知识留言
     */
    @Override
    public AppletKnowledgeComment selectAppletKnowledgeCommentById(Long id) {
        return appletKnowledgeCommentMapper.selectAppletKnowledgeCommentById(id);
    }

    /**
     * 查询知识留言列表
     *
     * @param appletKnowledgeComment 知识留言
     * @return 知识留言
     */
    @Override
    public List<AppletKnowledgeCommentVO> selectAppletKnowledgeCommentList(AppletKnowledgeCommentDTO appletKnowledgeComment) {
        List<Long> userIdList = appletUserService.getUserIdByUserName(appletKnowledgeComment.getUserName());
        List<AppletKnowledgeComment> appletKnowledgeComments = appletKnowledgeCommentMapper.selectAppletKnowledgeCommentList(appletKnowledgeComment, userIdList);
        List<AppletKnowledgeCommentVO> appletKnowledgeCommentVOList = Binder.convertAndBindRelations(appletKnowledgeComments, AppletKnowledgeCommentVO.class);
        return appletKnowledgeCommentVOList;
    }

    /**
     * 新增知识留言
     *
     * @param appletKnowledgeComment 知识留言
     * @return 结果
     */
    @Override
    public int insertAppletKnowledgeComment(AppletKnowledgeComment appletKnowledgeComment) {
        appletKnowledgeComment.setCreateTime(DateUtils.getNowDate());
        return appletKnowledgeCommentMapper.insertAppletKnowledgeComment(appletKnowledgeComment);
    }

    /**
     * 修改知识留言
     *
     * @param appletKnowledgeComment 知识留言
     * @return 结果
     */
    @Override
    public int updateAppletKnowledgeComment(AppletKnowledgeComment appletKnowledgeComment) {
        appletKnowledgeComment.setUpdateTime(DateUtils.getNowDate());
        return appletKnowledgeCommentMapper.updateAppletKnowledgeComment(appletKnowledgeComment);
    }

    /**
     * 批量删除知识留言
     *
     * @param ids 需要删除的知识留言主键
     * @return 结果
     */
    @Override
    public int deleteAppletKnowledgeCommentByIds(Long[] ids) {
        return appletKnowledgeCommentMapper.deleteAppletKnowledgeCommentByIds(ids);
    }

    /**
     * 删除知识留言信息
     *
     * @param id 知识留言主键
     * @return 结果
     */
    @Override
    public int deleteAppletKnowledgeCommentById(Long id) {
        return appletKnowledgeCommentMapper.deleteAppletKnowledgeCommentById(id);
    }

}
